package pro.cherish.knowledgemqrabbitmq.t1;


import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import pro.cherish.knowledgemqrabbitmq.util.RabbitUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author 创建人：cherish
 * @version 版本号：V1.0
 * <p>
 * ***************************修订记录************************************
 * 2019-10-27 cherish 创建该类功能。
 * **********************************************************************
 * </p>
 * @ClassName 类名：MsgProducer
 * @Description 功能说明：
 * <p>
 * TODO
 * </p>
 * **********************************************************************
 * @date 创建日期： 2019-10-27 cherish
 */
public class MsgProducer {
    public static void publishMsg(String exchange, BuiltinExchangeType exchangeType, String routingKey, String message) throws IOException, TimeoutException {
        ConnectionFactory factory = RabbitUtil.getConnectionFactory();
        // 创建连接
        Connection connection = factory.newConnection();
        //   创建通道
        Channel channel = connection.createChannel();
        //    声明exchange中的消息是可持久化，不自动删除
        channel.exchangeDeclare(exchange, exchangeType, true, false, null);
        //    发布消息
        channel.basicPublish(exchange, routingKey, null, message.getBytes());
        channel.close();
        connection.close();

    }


}
